﻿using Android.App;
using Android.Appwidget;
using Android.OS;
using Android.Util;
using System;
using Android.Content;
using Android.Hardware;
using Android.Views;
using Android.Widget;

namespace GBU.Services.Pedometer
{
    [Service]
    public class StepService : Service
    {
        private const string DebugTag = "StepService:";
        private const string TAG = "gbu.sports.pedometer.StepService";

        private static IBinder MBinder = null;

        private NotificationManager NoticeMng = null;

        public PowerManager.WakeLock MWakeLock = null;


        private SensorManager MSensorManager = null;
        private Sensor MSensor = null;
        private StepDetector MStepDetector = null;
        private StepDisplayer MStepDisplayer = null;

        private StepBroadcastReceiver MReceiver = null;

        public int MSteps;
        public int MPace;
        public float MSpeed;


        private MemStepListener MStepListener = null;
     
        public ICallback MCallback = null;//new StepServiceCallback(mHandler)

        public void RegisterCallback(ICallback callback)
        {
            MCallback = callback;
        }


        public override IBinder OnBind(Android.Content.Intent intent)
        {
            Log.Debug(DebugTag, "Service OnBind");
            MBinder = new StepBinder(this);
            return MBinder;
        }

        public override void OnCreate()
        {
            Log.Debug(DebugTag, "Service OnCreate");
            base.OnCreate();

           
        }

        public void RegisterDetector()
        {
            //目前只监测加速度传感器,后续可增加磁场传感器
            MSensor = MSensorManager.GetDefaultSensor(SensorType.Accelerometer);

            MSensorManager.RegisterListener(MStepDetector, MSensor, SensorDelay.Fastest);
        }

        public void UnRegisterDetector()
        {
            MSensorManager.UnregisterListener(MStepDetector);
        }

        public void AcquireWakeLock()
        {//根据用户设定,通知系统是否保持不关屏,不休眠
            PowerManager pm = (PowerManager)GetSystemService(Context.PowerService);
            WakeLockFlags wakeFlags;

            //默认不关屏
            wakeFlags = (WakeLockFlags.ScreenDim | WakeLockFlags.AcquireCausesWakeup);

            ////保持屏幕常亮
            //wakeFlags =  WakeLockFlags.ScreenDim;

            ////若未设置
            //wakeFlags =  WakeLockFlags.Partial;

            MWakeLock = pm.NewWakeLock(wakeFlags, TAG);
            MWakeLock.Acquire();
        }


        public override void OnStart(Android.Content.Intent intent, int startId)
        {
            Log.Debug(DebugTag, "Service Started");
            base.OnStart(intent, startId);
            //Do Something
            try
            {
                //var mState = GetSharedPreferences("state", 0);
                //NoticeMng = (NotificationManager) GetSystemService(Service.NotificationService);
                //ShowNotification();

                //保持屏幕亮度或关屏
              //  AcquireWakeLock();

                //开始检测步伐
                MStepDetector = new StepDetector();
                MSensorManager = (SensorManager)GetSystemService(Service.SensorService);
                RegisterDetector(); //将步伐监测器注册到系统

                //注册屏幕关闭receiver 到系统中,监控系统关屏操作
                MReceiver = new StepBroadcastReceiver(this);
                IntentFilter filter = new IntentFilter(Intent.ActionScreenOff);
                RegisterReceiver(MReceiver, filter);


                MStepListener = new MemStepListener(this);
                MStepDisplayer = new StepDisplayer();
                //MStepDisplayer.SetSteps(MSteps = mState.GetInt("steps", 0));
                MStepDisplayer.AddListener(MStepListener);
                MStepDetector.AddStepListener(MStepDisplayer);


                // Tell the user we started.
                Toast.MakeText(this, "开始计步...", ToastLength.Short).Show();
            }
            catch (Exception e)
            {
                Log.Debug(DebugTag, e.Message + "|" + e.StackTrace);
            }
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            Log.Debug(DebugTag, "Service Stoped");

            //Do Dispose methods
        }

        /// <summary>
        /// Show notification while service is running
        /// </summary>
        void ShowNotification()
        {


        }

        public StepService GetStepService()
        {
            return this;
        }

       
    }

    public interface ICallback
    {
         void StepsChanged(int value);
         void PaceChanged(int value);
         void DistanceChanged(float value);
         void SpeedChanged(float value);
         void CaloriesChanged(float value);

        
    }

    public class StepBinder : Binder
    {
        private StepService mStepService;

        public StepBinder(StepService stepService)
        {
            this.mStepService = stepService;
        }

        public StepService GetStepService()
        {
            return mStepService;
        }

    }


    public class MemStepListener : Listener
    {
        private StepService MStepService;
        public MemStepListener(StepService service)
        {
            MStepService = service;
        }

        public void StepsChanged(int value)
        {
            MStepService.MSteps = value;
            PassValue();
        }

        public void PassValue()
        {
            if (MStepService.MCallback != null)
            {
                MStepService.MCallback.StepsChanged(MStepService.MSteps);
            }
        }
    }

}
